/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.messagedriven;

import com.sdd.entities.Feature;
import com.sdd.entities.FeatureCategory;
import com.sdd.entities.Product;
import com.sdd.facades.business.FeatureBusinessFacadeBean;
import com.sdd.facades.business.FeatureCategoryBusinessFacadeBean;
import com.sdd.facades.business.ProductBusinessFacadeBean;
import com.sdd.plains.FeatureCategoryPlain;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 *
 * @author tpgalan
 */
@MessageDriven(mappedName = "jms/productQueue", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class ProductListener implements MessageListener {

    @EJB
    ProductBusinessFacadeBean productBusinessFacadeBean;
    @EJB
    FeatureBusinessFacadeBean fbfb;
    @EJB
    FeatureCategoryBusinessFacadeBean fcbfb;

    public ProductListener() {
    }

    @Override
    public void onMessage(Message message) {
        System.out.println("YEAHHHHHHHHH");
        if (message instanceof MapMessage) {
            Product p = new Product();
            MapMessage map = (MapMessage) message;
            try {
                p.setName(map.getString("name"));
                p.setDateAdded(new Date());
                p.setDescription(map.getString("descrption"));
                p.setPrice(map.getDouble("price"));
                p.setVendor(map.getString("vendor"));
                p.setStock(0);
                Product addProduct = productBusinessFacadeBean.addProduct(p);

                //features
                int nFeat = Integer.parseInt(map.getString("numberFeat"));
                for (int i = 0; i < nFeat; i++) {
                    FeatureCategory featureCategory = fcbfb.getFeatureCategory(map.getString("categoryFeature"+Integer.toString(i)));

                    if (featureCategory == null) {
                        FeatureCategoryPlain fcp = new FeatureCategoryPlain();
                        fcp.setName(map.getString("categoryFeature"+Integer.toString(i)));
                        featureCategory=fcbfb.addFeature(fcp);
                        
                    }
                    Feature f = new Feature();
                    f.setCategory(featureCategory);
                    f.setDescription(map.getString("description"+Integer.toString(i)));
                    f.setSummary(map.getString("summary"+Integer.toString(i)));
                    f.setProduct(addProduct);
                    fbfb.addFeature(f);

                }

                System.out.println("Toma features!!!!!!!!!!!!!!!!");

            } catch (JMSException ex) {
                Logger.getLogger(ProductListener.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
